package com.chat.chatting.service.impl;

import com.chat.chatting.dao.ApplicationDao;
import com.chat.chatting.entity.ApplicationEntity;
import com.chat.chatting.entity.UserEntity;
import com.chat.chatting.service.ApplicationService;
import com.chat.chatting.service.FriendService;
import com.chat.chatting.service.UserService;
import com.chat.chatting.util.UUIDUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Application)表服务实现类
 */
@Service("applicationService")
public class ApplicationServiceImpl implements ApplicationService {
    @Resource
    private ApplicationDao applicationDao;
    @Resource
    private FriendService friendService;
    @Resource
    private UserService userService;

//    /**
//     * 查询多条数据
//     */
//    @Override
//    public List<ApplicationEntity> getPage(Integer pageNum, Integer pageSize, Integer userID) {
//        if (pageNum == null) {
//            pageNum = 1;
//        }
//        if (pageSize == null) {
//            pageSize = 10;
//        }
//        Integer pre = pageSize * (pageNum- 1);
//        Integer latter = pre + pageNum;
//        if (userID == null || userID.equals(""))
//            return null;
//        return applicationDao.getPage(userID, pre, latter);
//    }

    @Override
    public List<ApplicationEntity> getUnsettledList(Integer pageNum, Integer pageSize, Integer userID) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Integer pre = pageSize * (pageNum- 1);
        Integer latter = pre + pageNum;
        if (userID == null || userID.equals(""))
            return null;
        return applicationDao.getUnsettledList(userID, pre, latter);
    }

    @Override
    public boolean apply(Integer applicantID, Integer byApplicantID,  String applicationRemark) {
        ApplicationEntity entity = applicationDao.selectUnsettled(applicantID, byApplicantID);
        UserEntity applicant = userService.queryByUserID(applicantID);
        if (entity == null) {
            entity = new ApplicationEntity();
            entity.setApplicantID(applicantID);
            entity.setApplicationRemark(applicationRemark);
            entity.setByApplicantID(byApplicantID);
            entity.setPkID(UUIDUtil.generateUUID());
            entity.setSendStatus(ApplicationEntity.UNSETTLED);
            entity.setApplicantNickName(applicant.getNickName());
            entity.setApplicantPhoto(applicant.getPhoto());
            applicationDao.apply(entity);
        } else {
            //如果是已经申请过了，修改备注信息
            entity.setApplicationRemark(applicationRemark);
            entity.setApplicantNickName(applicant.getNickName());
            entity.setApplicantPhoto(applicant.getPhoto());
            applicationDao.updateRemark(entity);
        }
        return true;
    }

    @Override
    public boolean reject(Integer applicantID, Integer byApplicantID) {
        ApplicationEntity entity = applicationDao.selectUnsettled(applicantID, byApplicantID);
        if (entity == null) return false;
        entity.setSendStatus(ApplicationEntity.DISAGREE);
        applicationDao.updateSendStatus(entity);
        return true;
    }

    @Override
    public boolean agree(Integer applicantID, Integer byApplicantID) {
        if(applicantID == null || applicantID.equals(""))
            return false;
        if(byApplicantID == null || applicantID.equals(""))
            return false;
        ApplicationEntity entity = applicationDao.selectUnsettled(applicantID, byApplicantID);
        if (entity == null)
            return false;
        entity.setSendStatus(ApplicationEntity.AGREE);
        applicationDao.updateSendStatus(entity);
        //好友表新好友
        friendService.create(applicantID,byApplicantID);
        friendService.create(byApplicantID,applicantID);
        return true;
    }


}